from django.shortcuts import render
from django.http import JsonResponse
from rest_framework.parsers import JSONParser
from rest_framework.decorators import api_view
from .models import User
from django.core.paginator import Paginator
from university_management_system import utils


# 显示所有数据(分页)
@api_view(['get'])
def search(request):
    page = request.GET.get('page', 1)
    pagesize = request.GET.get('pagesize', 5)
    username = request.GET.get('username')
    promiss = request.GET.get('promiss')
    dictdata = {}
    if not utils.is_empty(username):
        dictdata['username'] = username
    if not utils.is_empty(promiss):
        dictdata['promiss'] = promiss

    userobj = User.objects.filter(**dictdata).values()

    userdata = Paginator(userobj, pagesize)

    total = userobj.count()

    pageobj = userdata.page(page)
    datalist = []
    for pro in pageobj.object_list:
        datalist.append(pro)
    return JsonResponse({"code": "1001", "message": "查询成功", "data": datalist, "total": total})


# 添加修改管理员
@api_view(['post'])
def adduser(request):
    token = request.headers.get('token')
    if utils.is_empty(token):
        return JsonResponse({"code": "-1003", "message": "请先登录"})
    payload = utils.decode_token(token)
    promiss = payload['promiss']
    if not promiss == '管理员':
        # 解析请求数据
        data = JSONParser().parse(request)
        id = data.get('id')
        if utils.is_empty(id):
            User(**data).save()
            return JsonResponse({"code": "1001", "message": "添加成功"})
        user = User.objects.filter(id=id)
        del data['id']
        user.update(**data)
        return JsonResponse({"code": "1001", "message": "修改成功"})
    return JsonResponse({"code": "-1001", "message": "权限不足,请联系管理员"})


# 登录，生成token令牌
@api_view(['post'])
def login(request):
    data = JSONParser().parse(request)
    username = data.get("username")
    password = data.get("password")
    userobj = User.objects.filter(**data)
    if userobj:
        promiss = userobj.values().first().get("promiss")
        token = utils.encode_token(username=username, password=password, promiss=promiss)
        return JsonResponse({"code": "1001", "message": "登录成功", "token": token})

    return JsonResponse({"code": "-1001", "message": "该用户不存在", "token": ""})


@api_view(['get'])
def personal(request):
    token = request.headers.get('token')
    if utils.is_empty(token):
        return JsonResponse({"code": "-1003", "message": "请先登录"})
    payload = utils.decode_token(token)
    userobj = User.objects.filter(**payload).values().first()
    return JsonResponse({"code": "1001", "message": "查询成功", "data": userobj})

@api_view(['delete'])
def delete(request,id):
    token = request.headers.get('token')
    if utils.is_empty(token):
        return JsonResponse({"code": "-1003", "message": "请先登录"})
    payload = utils.decode_token(token)
    promiss = payload['promiss']
    if promiss == '超级管理员':
        userobj = User.objects.filter(id=id)
        if userobj:
            userobj.delete()
            return JsonResponse({"code": "1001", "message": "删除成功"})
        return JsonResponse({"code": "-1001", "message": "该用户信息不存在"})

    return JsonResponse({"code": "-1001", "message": "权限不足,请联系管理员"})
